//导入express
const express = require("express")
//引入数据库对象
const connection = require(`${__dirname}/../mysql/index.js`)
//创建路由对象
const router = express.Router()

//查询文章
/*参数
* sql 数据库语句
* res 响应对象*/
function selectArticle(sql,res){
    connection.query(sql,(err,result)=>{
        if(err){
            console.log(err)
            res.send({code: 1,data: {},msg: "查询失败"})
        }else{
            const articleList = []
            result.map(item=>{
                const data = {
                    bid: item.b_id,
                    name: item.b_name,
                    content: item.b_content,
                    category: item.b_category
                }
                articleList.push(data)
            })
            res.send({code: 0,data: {articleList},msg: "查询成功"})
        }
    })
}

//查询文章
/*参数
* bid int 可选 根据文章id查询文章
* name String 可选 根据文章名查询
* category String 可选 根据文章所属查询
* 若无传递参数则查询所有文章*/
router.get("/get_article",(req,res)=>{
    const {bid,name,category} = req.query
    const sql = `select * from books where 1 = 1 ${bid ? `and b_id = ${bid}` : ""} ${name ? `and b_name = "${name}"` : ""} ${category ? `and b_category = "${category}"` : ""}`
    selectArticle(sql,res)
})

//添加文章
/*参数
* name String 必传 文章名
* content String 必传 文章内容
* category String 必传 文章所属*/
router.post("/add_article",(req,res)=>{
    const {name,content,category} = req.body
    if(name === undefined || content === undefined || category === undefined) {
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `insert into books(b_name,b_content,b_category)
                values("${name}","${content}","${category}")`
    connection.query(sql,err=>{
        if(err){
            console.log(err)
            res.send({code: 1,data: {},msg: "添加失败"})
        }else{
            res.send({code: 0,data: {},msg: "添加成功"})
        }
    })
})

//删除文章
/*参数
* bid int 必传 根据文章id删除*/
router.get("/delete_article",(req,res)=>{
    const {bid} = req.query
    if(bid === undefined){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `delete from books where b_id = ${bid}`
    connection.query(sql,err=>{
        if(err){
            res.send({code: 1,data: {},msg: "删除失败"})
        }else{
            res.send({code: 0,data: {},msg: "删除成功"})
        }
    })
})

//修改文章
/*参数
* bid int 必传 根据文章id修改内容
* name String 必传 文章名
* content String 必传 文章内容
* category String 必传 文章所属*/
router.post("/update_article",(req,res)=>{
    const {bid,name,content,category} = req.body
    if(bid === undefined || name === undefined || content === undefined || category === undefined) {
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `update books set b_name = "${name}",b_content = "${content}",b_category = "${category}" where b_id = ${bid}`
    connection.query(sql,err=>{
        if(err){
            res.send({code: 1,data: {},msg: "更新失败"})
        }else{
            res.send({code: 0,data: {},msg: "更新成功"})
        }
    })
})

//搜索相关文章
/*参数
* keyWord String 必传 关键字*/
router.get("/search_article",(req,res)=>{
    const {keyWord} = req.query
    if(keyWord === undefined || keyWord === ""){
        return res.send({code: 1, data: {}, msg: "必传参数缺失"})
    }
    const sql = `select * from books where b_name like "%${keyWord}%" or b_category like "%${keyWord}%"`
    selectArticle(sql,res)
})

module.exports = router